﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using NUnit.Framework;
using test.XcoAppSpaces.Core.Configuration.TestInterpreters;
using XcoAppSpaces.Contracts.Transport;
using XcoAppSpaces.Core.Configuration;
using XcoAppSpaces.Transport.Sockets;
using TypeResolve.Loader_Net45;

namespace test.XcoAppSpaces.Core.Configuration
{
    [TestFixture]
    public class testSpaceStringConfigurator
    {
    	[SetUp]
    	public void SetUp()
    	{
			AssemblyLoader.ConfigurePlatformBasedAssemblyLoading();
    		DefaultInterpreters.CustomConfigStringInterpreterTypes.Clear();
    	}

        [Test]
        public void LoadCustomConfigStringInterpreters_FindNewOne()
        {
			var sw = new Stopwatch();
			sw.Start();
            var list = SpaceStringConfigurator.LoadCustomConfigStringInterpreterTypes().ToList();
			sw.Stop();
			Console.WriteLine("LoadCustomConfigStringInterpreters duration: " + sw.ElapsedMilliseconds);

			Assert.AreEqual(1, list.Count);
			Assert.AreEqual(typeof(TestInterpreter1), list[0]);
        }

		[Test]
		public void LoadCustomConfigStringInterpreters_AlreadyAdded()
		{
			DefaultInterpreters.CustomConfigStringInterpreterTypes.Add(typeof(TestInterpreter1));
			var sw = new Stopwatch();
			sw.Start();
			var list = SpaceStringConfigurator.LoadCustomConfigStringInterpreterTypes().ToList();
			sw.Stop();
			Console.WriteLine("LoadCustomConfigStringInterpreters duration: " + sw.ElapsedMilliseconds);

			Assert.AreEqual(0, list.Count);
		}

		[Test]
		public void CreateConfig_KnownKeys()
		{
			var sw = new Stopwatch();
			sw.Start();
			var config = SpaceStringConfigurator.CreateConfig("tcp.port=0");
			sw.Stop();
			Console.WriteLine("CreateConfig duration: " + sw.ElapsedMilliseconds);

			Assert.AreEqual(1, config.ServiceConfigs[typeof(IXcoTransportService)].Count);
			Assert.IsInstanceOf(typeof(XcoTCPTransportService), config.ServiceConfigs[typeof(IXcoTransportService)][0].ConfiguredService);
		}

		[Test]
		public void CreateConfig_CustomUnknownKeys()
		{
			var sw = new Stopwatch();
			sw.Start();
			var config = SpaceStringConfigurator.CreateConfig("test1.key1=testvalue");
			sw.Stop();
			Console.WriteLine("CreateConfig duration: " + sw.ElapsedMilliseconds);

			Assert.AreEqual(1, config.ServiceConfigs[typeof(TestService1)].Count);
			Assert.IsInstanceOf(typeof(TestService1), config.ServiceConfigs[typeof(TestService1)][0].ConfiguredService);
		}

		[Test]
		public void CreateConfig_CustomKnownKeys()
		{
			DefaultInterpreters.CustomConfigStringInterpreterTypes.Add(typeof(TestInterpreter1));
			var config = SpaceStringConfigurator.CreateConfig("test1.key1=testvalue");
			Assert.AreEqual(1, config.ServiceConfigs[typeof(TestService1)].Count);
			Assert.IsInstanceOf(typeof(TestService1), config.ServiceConfigs[typeof(TestService1)][0].ConfiguredService);
		}
    }
}
